import { User } from '../models/userModel';

export class UserService {
  // 创建用户
  static async createUser(userData: {
    username: string;
    email: string;
    password: string;
    role?: string;
  }) {
    try {
      const user = await User.create(userData);
      return { success: true, data: user };
    } catch (error: unknown) {
      const errorMessage = error instanceof Error ? error.message : '未知错误';
      return { success: false, error: errorMessage };
    }
  }

  // 获取所有用户
  static async getAllUsers() {
    try {
      const users = await User.findAll({
        attributes: { exclude: ['password'] }, // 排除密码字段
      });
      return { success: true, data: users };
    } catch (error: unknown) {
      const errorMessage = error instanceof Error ? error.message : '未知错误';
      return { success: false, error: errorMessage };
    }
  }

  // 根据ID获取用户
  static async getUserById(id: number) {
    try {
      const user = await User.findByPk(id, {
        attributes: { exclude: ['password'] },
      });
      if (!user) {
        return { success: false, error: '用户不存在' };
      }
      return { success: true, data: user };
    } catch (error: unknown) {
      const errorMessage = error instanceof Error ? error.message : '未知错误';
      return { success: false, error: errorMessage };
    }
  }

  // 更新用户
  static async updateUser(id: number, updateData: Partial<User>) {
    try {
      const user = await User.findByPk(id);
      if (!user) {
        return { success: false, error: '用户不存在' };
      }
      
      // 排除密码字段，密码更新需要单独处理
      const { password, ...dataToUpdate } = updateData;
      await user.update(dataToUpdate);
      
      return { success: true, data: user };
    } catch (error: unknown) {
      const errorMessage = error instanceof Error ? error.message : '未知错误';
      return { success: false, error: errorMessage };
    }
  }

  // 删除用户
  static async deleteUser(id: number) {
    try {
      const user = await User.findByPk(id);
      if (!user) {
        return { success: false, error: '用户不存在' };
      }
      
      await user.destroy();
      return { success: true, message: '用户删除成功' };
    } catch (error: unknown) {
      const errorMessage = error instanceof Error ? error.message : '未知错误';
      return { success: false, error: errorMessage };
    }
  }

  // 根据用户名查找用户
  static async getUserByUsername(username: string) {
    try {
      const user = await User.findOne({
        where: { username },
      });
      return { success: true, data: user };
    } catch (error: unknown) {
      const errorMessage = error instanceof Error ? error.message : '未知错误';
      return { success: false, error: errorMessage };
    }
  }

  // 根据邮箱查找用户
  static async getUserByEmail(email: string) {
    try {
      const user = await User.findOne({
        where: { email },
      });
      return { success: true, data: user };
    } catch (error: unknown) {
      const errorMessage = error instanceof Error ? error.message : '未知错误';
      return { success: false, error: errorMessage };
    }
  }
} 